package org.medicalinstrument.service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.medicalinstrument.bean.PageBean;
import org.medicalinstrument.bean.Product;
import org.medicalinstrument.dao.ProductDao;
import org.medicalinstrument.impl.ProductDaoImpl;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: kongHuan
 * @Date: 2019/3/9 20:58
 * @Description:
 */
public class ProductService {
    private ProductDao dao = new ProductDaoImpl();
    private String returnString;

    /**
     * 上传商品
     **/
    public String addProduct(Product pro) throws SQLException {

        if (dao.addProduct(pro)){
            return "insert_success";
        }else {
            return "insert_error";
        }
    }

    /*
     * 通过二维码获取商品信息
     */
    public String getByProductQR(String productQR) {
        Product product = null;
        try {
            product = dao.getByProductQR(productQR);
            returnString = JSONObject.fromObject(product).toString();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }


    /**
     * 删除商品
     */
 /*   public String delByProductQR(String productQR) {
        try {
            System.out.println(productQR);
            dao.deleteByProductQR(productQR);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }*/

    /**
     *
     * 修改商品信息
     */
    public String updateProduct(Product pro) {
        try {
            dao.updateByProductQR(pro);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "insert_success";
    }

    public String showByPage(PageBean page, String conType, String conName) {
        try {
            int totalCount = dao.totalCount(conType,conName);
            page.setTotalCount(totalCount);
            page.setTotalPage((totalCount % page.getRows()) == 0 ? (totalCount/page.getRows()) : (totalCount/page.getRows())+1);
            page.setList(dao.showByPage(page,conType,conName));
//            if(!CollectionUtils.isEmpty(page.getList())){
//                returnString = JSONObject.fromObject(page).toString();
//            }
            returnString = JSONObject.fromObject(page).toString();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }
    public String showByPage1(PageBean page, String conType, String conName) {
        try {
            int totalCount = dao.totalCount(conType,conName);
            page.setTotalCount(totalCount);
            page.setTotalPage((totalCount % page.getRows()) == 0 ? (totalCount/page.getRows()) : (totalCount/page.getRows())+1);
            page.setList(dao.showByPage1(page,conType,conName));
//            if(!CollectionUtils.isEmpty(page.getList())){
//                returnString = JSONObject.fromObject(page).toString();
//            }
            returnString = JSONObject.fromObject(page).toString();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }

    public String getAll() {
        List<Product> products = null;
        try {
            products = dao.getAllProduct();
            if (products != null && products.size() > 0) {
                returnString = JSONArray.fromObject(products).toString();
            } else {
                returnString = "noproduct";
                //System.out.println(returnString);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return returnString;
    }

    public String delByProductQR(String productQR) {
        try {
            System.out.println(productQR);
            dao.deleteByProductQR(productQR);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }

    public int brokenTotal() {
        int brokenCount = 0;
        try {
             brokenCount = dao.brokenCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return brokenCount;
    }
}
